<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-col :span="24">
            <a-form-item label="物料编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-mat
                v-decorator="['matCode', validatorRules.matCode]"
                :multiple="false"
                @callback="MatCallback"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['matName']" placeholder="请输入物料名称" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['standard']" placeholder="请输入规格" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['matUnit']" placeholder="请输入单位" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="长度(M)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['lenght']" :precision="2" placeholder="请输入长度" @keyup="lenghtChange()" style="width: 100%"></a-input-number>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="重量(KG)" :precision="2" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['weight']" :precision="2" placeholder="请输入重量" @keyup="weightChange()" style="width: 100%"></a-input-number>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="数量" :precision="2" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['count', validatorRules.count]" placeholder="请输入数量" @keyup="countChange()" style="width: 100%"></a-input-number>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="总长(m)" :precision="2" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['totalLenght']" :precision="2" placeholder="请输入总长(m)" disabled style="width: 100%"></a-input-number>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="总重(kg)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number :precision="2" v-decorator="['totalWeight']" placeholder="请输入总重(kg)" disabled style="width: 100%"></a-input-number>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JSelectMat from '@/views/components/JSelectMat'

  export default {
    name: "RequirementModal",
    components: {
      JSelectMat
    },
    props:{
      mainId:{
        type:String,
        required:false,
        default:''
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
          matCode: {
            rules: [
              { required: true, message: '请输入物料编号!'},
            ]
          },
          count: {
            rules: [
              { required: true, message: '请输入数量!'},
            ]
          },
        },
        url: {
          add: "/engine/engineering/addRequirement",
          edit: "/engine/engineering/editRequirement",
        }

      }
    },
    created () {
    },

    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'matId','matCode','matName','matUnit','standard','lenght','weight','count','totalLenght','totalWeight','engineeringId','createBy','createTime','updateBy','updateTime','sysOrgCode'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            formData['engineeringId'] = this.mainId
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'matId','matCode','matName','matUnit','standard','lenght','weight','count','totalLenght','totalWeight','engineeringId','createBy','createTime','updateBy','updateTime','sysOrgCode'))
      },
      MatCallback(row){
        this.form.setFieldsValue({
          matId:row[0].id,
          matCode:row[0].matCode,
          matName:row[0].matName,
          standard:row[0].matStandard,
          matUnit:row[0].matUnit,
          lenght:row[0].matLenght,
          weight:row[0].matWeight
        });
      },
      weightChange(){
        let count = this.form.getFieldValue("count")
        let weight = this.form.getFieldValue("weight")
        if (count!== undefined && count !=='' && weight!==undefined && weight !==''){
          this.form.setFieldsValue({"totalWeight":parseInt(count)*parseFloat(weight)})
        }
      },
      lenghtChange(){
        let count = this.form.getFieldValue("count")
        let lenght = this.form.getFieldValue("lenght")
        if (count!== undefined && count !=='' && lenght!==undefined && lenght !==''){
          this.form.setFieldsValue({"totalLenght":parseInt(count)*parseFloat(lenght)})
        }
      },
      countChange(){
        let count = this.form.getFieldValue("count")
        let lenght = this.form.getFieldValue("lenght")
        if (count!== undefined && count !=='' && lenght!==undefined && lenght !==''){
          this.form.setFieldsValue({"totalLenght":parseInt(count)*parseFloat(lenght)})
        }
        let weight = this.form.getFieldValue("weight")
        if (count!== undefined && count !=='' && weight!==undefined && weight !==''){
          this.form.setFieldsValue({"totalWeight":parseInt(count)*parseFloat(weight)})
        }
      }


    }
  }
</script>
